<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>选择组织用户</title>
	<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
	<div class="searchArea">
		<input name="isMore" id="isMore" type="text" style="display: none;" class="ui-textbox" value="${isMore}">
		<form id="searchBar">
			<div class="fieldset1 clearFix FBL5">
				<div class="fieldBox">
					<div class="label">姓名</div>
					<div class="field">
						<input name="rlNm" id="rlNm" type="text" class="ui-textbox" value="" ligerui="width: 140">
					</div>
				</div>
				<div class="fieldBox">
					<div class="label">工号</div>
					<div class="field">
						<input name="usrNm" id="usrNm" type="text" class="ui-textbox" value="" ligerui="width: 140">
					</div>
				</div>
				<span id="searchBtn" class="sq-button sq-button-round">查询</span>
				<span id="cleanFormBtn" class="sq-button sq-button-round">清除</span>
			</div>
		</form>
	</div>
	<table class="layout2col1">
		<tbody>
		<tr>
			<td width="300" valign="top">
				<div class="sqPanel">
					<div class="sqPanel_head">
						<form id="searchBar2" class="titSearchBar">
							<div class="fieldset1 clearFix">
								<div class="fieldBox">
									<div class="label"><i class="sqIco sqIco_3"></i>组织</div>
									<div class="field">
										<input name="orgNm" id="orgNm" type="text" class="ui-textbox" value="" ligerui="width: 140">
									</div>
								</div>
								<span id="searchBtn2" class="sq-search-button"></i>查询</span>
							</div>
						</form>
					</div>
					<div class="sqPanel_body">
						<div class="btnsBox">&nbsp;</div>
						<div class="nanoWrap" style="height: 330px;">
							<div class="nano"><div class="nano-content">
								<ul id="tree1">
								</ul>
							</div></div>
						</div>
					</div>
				</div>
			</td>
			<td valign="top">
				<div class="sqPanel">
					<div class="sqPanel_head">
						<span>检索结果</span>
					</div>
					<div class="sqPanel_body">
						<div id="ligergrid" class="sq-grid"></div>
						<div id="selecetedItems" class="textareaBox Hide" style="margin-top:10px; height:60px;"></div>
					</div>
				</div>
				<div class="sqPanel">
					<div class="sqPanel_head">
						<span>已选内容</span>
					</div>
					<div class="sqPanel_body">
						<div id="ligergrid_hx" class="sq-grid"></div>
					</div>
				</div>
			</td>
		</tr>
		</tbody>
	</table>
	<div class="btnsBox TC">
		<span id="submitBtn" class="sq-button sq-button-round">确定</span>
		<span id="clearAllBtn" class="sq-button sq-button-round">清除</span>
		<span id="cancelBtn" class="sq-button sq-button-round"></i>关闭</span>
	</div>
</div>

<script>
	var R_url='../../_app/';
	var tree,
			grid,
			grid_addPE;
	combo_DATA=[];
	$(function () {
		initOrgTree();
		initForm();
		render_ligergrid_hx();
		initSearchEvent();
		initClearLigerFormEvent();
		initSearchBarOrgForm();
		initSearchBarOrgFormEvent();
		initSubmitBtnEvent();
		initClearAllBtnEvent();
		initCancelBtnEvent();
	});

	//取消按钮
	function initCancelBtnEvent(){
		$("#cancelBtn").click(function(event) {
			frameElement.dialog.close();
		});
	}

	//初始化清除按钮Btn
	function initClearAllBtnEvent(){
		$("#clearAllBtn").click(function(event) {
			$("#cleanFormBtn").click();
			tree.reload();
			if (grid) {
				reLoadGrid();
			}
			change_combo_DATA("clear");
		});
	}

	//提交按钮事件
	function initSubmitBtnEvent(){
		$("#submitBtn").click(function(event) {
			if(!grid || grid.getSelecteds().length==0) {
				$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "error"});
				return false; //验证不通过，不执行以下代码
			}
			var data=[];
			for (var i = 0; i < combo_DATA.length; i++) {
				var combo_item=combo_DATA[i];
				for(key in combo_item){
					data.push(key);
				}
			};

			$.ligerDialog.SQconfirm(LogUtils.LOG_CMM_SUBMIT_CONFIRM_CONTENT_001,function (yes) {
				if (yes==false) return;
				if(frameElement && frameElement.dialog) {
					frameElement.dialog._DATA={usrIds: data}; //给父窗口的dialog传参
					frameElement.dialog.set("_usrs", combo_DATA); //给父窗口的dialog传参
					frameElement.dialog.close();
				}
			});
		});
	}

	//初始化查询组织机构查询Form
	function initSearchBarOrgForm(){
		$("#searchBar2").ligerForm();
	}

	//初始化查询组织机构事件
	function initSearchBarOrgFormEvent(){
		$("#searchBtn2").click(function() {
			var form = liger.get("searchBar2");
			var data = form.getData();
			searchOrgTreeByOrgNm(data);
		});
	}

	//清除按钮事件
	function initClearLigerFormEvent(){
		$("#cleanFormBtn").click(function(event) {
			clearLigerForm("searchBar");
		});
	}

	//初始化查询事件
	function initSearchEvent(){
		$("#searchBtn").click(function(event) {
			var form = liger.get("searchBar");
			var data = form.getData();
			var selectArry = tree.getChecked();
			var arr = [];
			for (var i = 0; i < selectArry.length; i++) {
				arr[i] = selectArry[i].data.id;
			}
			if (nodeId) {
				arr.push(nodeId);
			}else {
				return;
			}
			var selectIds = arr.join(",");
			data.orgId = selectIds;
			//加上当前组织
			set_ligergrid(data);
		});
	}

	//重新加载GRID
	var nodeId = null;
	function reLoadGrid(){
		var form = liger.get("searchBar");
		var data = form.getData();
		var selectArry = tree.getChecked();
		var arr = [];
		for (var i = 0; i < selectArry.length; i++) {
			arr[i] = selectArry[i].data.id;
		}
		if (nodeId) {
			arr.push(nodeId);
		}else {
			return;
		}
		var selectIds = arr.join(",");
		data.orgId = selectIds;
		//加上当前组织
		set_ligergrid(data);
	}

	//初始化查询Bar
	function initForm(){
		$("#searchBar").ligerForm();
	}

	//初始化组织树
	function initOrgTree(){
		tree = $("#tree1").ligerTree({
			url: '<%=path%>/system/sysOrg/initList.do',
			ajaxType: 'get',
			checkbox: true,
			parentIcon: "SqFolder",
			childIcon: "SqLeaf",
			needCancel:false,
			isExpand: true,
			isLeaf: function (data) {
				if (!data) return false;
				if (data.hasChildren) {return false} else if(data.children){return false} else {return true};
			},
			delay: function(e)
			{

			},
			onAfterAppend: function (parentNode, newdata) {
				setNanoScroller();
			},
			onExpand: function (node) {
				setNanoScroller();
			},
			onCollapse: function (node) {
				setNanoScroller();
			},
			onContextmenu: function (node, e) {
				actionNode=node;
				var parentNode = tree.getParent(node);
				if (parentNode) {
					treeMenu2.show({ top: e.pageY, left: e.pageX });
				}else{  //没有父节点，那就是根节点
					//treeMenu2.hide();
					treeMenu.show({ top: e.pageY, left: e.pageX });
				};
				return false;
			},
			onSelect: function (node) {
				nodeId = node.data.id;//node加入全局变量
				reLoadGrid();
			},
			onCancelselect: function (data,target) {
				if (grid) {
				}
			},
			onSuccess: function (data) {
				setNanoScroller();
				sss = data;
				tree.selectNode(data[0], true, false);
			}
		});
	}

	//通过查询条件获取Tree数据
	function searchOrgTreeByOrgNm (parms) {
		var _url = '<%=path%>/system/sysOrg/searchOrgTree.do';
		AjaxUtils.ajaxGet(_url, parms, function(msg){
			tree.setData(data.data);
		},true);

	}

	//重新处理Grid
	function set_ligergrid (parms) {
		if (grid) {
			grid.grid.show();
			grid.set('parms', {});
			for (name in parms) {grid.setParm(name, parms[name])};
			grid.reload();
		}else{
			render_ligergrid (parms);
			render_ligergrid_hx();
		};
	}

	//渲染Grid
	function render_ligergrid (parms) {
		grid = $("#ligergrid").ligerGrid({
			url: "<%=path%>/system/sysUsr/searchFunAuthUsrByOrgId.do?usrTypCd=1",
			parms: parms,
			method : 'get',
			fixedCellHeight: false,
			checkbox: true,
			frozenCheckbox: false,
			allowHideColumn: false,
			columns: [{
				display: '中文名',
				name: 'refRlNm',
				width: 100
			}, {
				display: '账号',
				name: 'usrNm',
				width: 150
			}, {
				display: '角色',
				name: 'roleNm',
				width: 100
			}, {
				display: '组织',
				name: 'orgCdNm',
				width: 200
			}],
			pageSize:10,
			width: '100%',
			isScroll: false,
			root:'rows',
			record:'total',
			hideLoadButton : true,
			onSelectRow: function (rowdata, rowid, rowobj, j) {
				var obj={};
				var valueObj = {};
				valueObj["usrId"] = rowdata.id;
				valueObj["usrNm"] = rowdata.usrNm;
				valueObj["rlNm"] = rowdata.refRlNm;
				valueObj["orgCdNm"] = rowdata.orgCdNm;
				valueObj["orgCd"] = rowdata.orgCd;
				obj[rowdata.id]=valueObj;
				if(grid.options.isSingleCheck) change_combo_DATA("clear");
				change_combo_DATA("add", obj);
			},
			onUnSelectRow: function (rowdata, rowid, rowobj) {
				var obj={};
				var valueObj = {};
				valueObj["usrId"] = rowdata.id;
				valueObj["usrNm"] = rowdata.usrNm;
				valueObj["rlNm"] = rowdata.refRlNm;
				valueObj["orgCdNm"] = rowdata.orgCdNm;
				valueObj["orgCd"] = rowdata.orgCd;
				obj[rowdata.id]=valueObj;
				change_combo_DATA("delete", obj);
			},
			onAfterShowData: function() {
				SetIframe_height();
			}
		});
		//开启不同模式,默认模式为多选
		var isMore = $("#isMore").val();
		if(isMore == 0){
			grid.options.isSingleCheck=true;
		}
	}

	//渲染Grid_hx
	function render_ligergrid_hx () {
		var sysUsrVOS = JsonUtils.parse('${sysUsrVOS}');
		grid_hx = $("#ligergrid_hx").ligerGrid({
			data: JsonUtils.parse('${sysUsrVOS}'),
			columns: [{
				display: '中文名',
				name: 'rlNm',
				width: 100
			}, {
				display: '账号',
				name: 'usrNm',
				width: 100
			}, {
				display: '组织',
				name: 'orgCdNm',
				width: 200
			}],
			width: '100%',
			height: 200,
			root: 'rows',
			isScroll: true,
			usePager: false,
			alternatingRow: true
		});
		grid_hx.setHeight(200);
		grid_hx.reload();
	}


	function change_combo_DATA (method, obj) {
		if (method=="add") {
			for (var i = 0; i < combo_DATA.length; i++) {
				if(JSON.stringify(combo_DATA[i])==JSON.stringify(obj)) {
					top.$.ligerDialog.SQalert({content: LogUtils.LOG_FLOW_PERSON_ALWAYS_HERE, SQ_type: "warning"});
					return false;
				}
			};
			combo_DATA.push(obj);
		}
		if (method=="delete") arrDel(obj, combo_DATA);
		if (method=="clear") combo_DATA.length=0;
		view_combo_DATA ();
	}
	function view_combo_DATA () {
		if (combo_DATA.length==0) {
			$("#selecetedItems").addClass('Hide');
			return false;
		};
		var selecetedItems_h=[]
		for (var i = 0; i < combo_DATA.length; i++) {
			var selecetedItem=combo_DATA[i];
			for(key in selecetedItem){
				selecetedItems_h.push('<span>'+selecetedItem[key].rlNm+'</span>, ')
			}
		};
		$("#selecetedItems").removeClass('Hide').html(selecetedItems_h.join(''))
	}
	function arrDel (obj,arr) {
		for (var i = 0; i < arr.length; i++) {
			if(JSON.stringify(arr[i])==JSON.stringify(obj)) break;
		};
		arr.splice(i,1);
		return arr;
	}
	function setNanoScroller () {
		$(".nanoWrap .nano").nanoScroller({
			preventPageScrolling: true
		});
	}
	SET_popupIframeHeight();
</script>
</body>
</html>